﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Diagnostics;
using System.Windows.Forms;
using Contract;
using System.IO;

namespace client
{
    public partial class frmClient : Form
    {
        string pcn;
        public frmClient(string PCN)
        {
            InitializeComponent();
            RefreshFileList();
            
            // create a hosting process for Service
            ServiceHost host = new ServiceHost(typeof(CallBack));
            NetTcpBinding binding = new NetTcpBinding();
            binding.TransferMode = TransferMode.Buffered;
            binding.ReceiveTimeout = TimeSpan.MaxValue;
            binding.MaxReceivedMessageSize = int.MaxValue;

            host.AddServiceEndpoint(typeof(ICallBack), binding,
            new Uri("net.tcp://127.0.0.1:8000/CallBack"));
            this.pcn = PCN;
            host.Open();
            CallBack.handler = RefreshFileList;
        }

        private void btnUpload_Click(object sender, EventArgs e)
        {
            //////////////////////////////////////
            NetTcpBinding binding = new NetTcpBinding();
            binding.TransferMode = TransferMode.Streamed;
            binding.SendTimeout = TimeSpan.MaxValue;
            binding.MaxReceivedMessageSize = 9223372036854775807;

            EndpointAddress address = new EndpointAddress("net.tcp://localhost:5000/DataUploader");

            ChannelFactory<IDataUploader> channel = new ChannelFactory<IDataUploader>(binding, address);
            IDataUploader Proxy = channel.CreateChannel();
            ///////////////////////////////////////////////
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.ShowDialog();
            string filename = Path.GetFileName(dialog.FileName);
            Stream uploadStream = new FileStream(dialog.FileName, FileMode.Open);
            MyFile moo = new MyFile() { Value = uploadStream, Name = filename, Size = uploadStream.Length };
            try
            {
               // Proxy.Upload(moo,PCN);
            }
            catch
            { MessageBox.Show("exception"); }
            ((IClientChannel)Proxy).Close();
           // RefreshFileList();
        }


        public void RefreshFileList(MyFile[] fl)
        {
            FileList.Items.Clear();

            foreach (var file in fl)
            {
                ListViewItem item = new ListViewItem(file.Name);

                item.SubItems.Add(Convert.ToString(file.Size));
                item.SubItems[1].Text = Convert.ToString(file.Size);

                FileList.Items.Add(item);
            }
        }

        private void RefreshFileList()
        {
            ////////////////////////////////////
            NetTcpBinding binding = new NetTcpBinding();
            binding.TransferMode = TransferMode.Streamed;
            binding.SendTimeout = TimeSpan.MaxValue;

            EndpointAddress address = new EndpointAddress("net.tcp://localhost:5000/DataUploader");

            ChannelFactory<IDataUploader> channel =
            new ChannelFactory<IDataUploader>(binding, address);
            IDataUploader Proxy = channel.CreateChannel();
            //////////////////////////////////////////////

            MyFile[] mfs = null;

            mfs = Proxy.Getfilesinfo(null);

            FileList.Items.Clear();

            foreach (var file in mfs)
            {
                ListViewItem item = new ListViewItem(file.Name);

                item.SubItems.Add(Convert.ToString(file.Size));
                item.SubItems[1].Text = Convert.ToString(file.Size);

                FileList.Items.Add(item);
            }
        }

        private void btnDownload_Click(object sender, EventArgs e)
        {
            ////////////////////////////////////
            NetTcpBinding binding = new NetTcpBinding();
            binding.TransferMode = TransferMode.Streamed;
            binding.MaxReceivedMessageSize = 200000000000000;
            binding.SendTimeout = TimeSpan.MaxValue;

            EndpointAddress address = new EndpointAddress("net.tcp://localhost:5000/DataUploader");

            ChannelFactory<IDataUploader> channel =
                new ChannelFactory<IDataUploader>(binding, address);
            IDataUploader Proxy = channel.CreateChannel();
            //////////////////////////////////////////////


            if (FileList.SelectedItems.Count == 0)
            {
                MessageBox.Show("No File is Selected");
            }
            else
            {
                ListViewItem item = FileList.SelectedItems[0];

                string path = item.SubItems[0].Text;

                SaveFileDialog dialog = new SaveFileDialog()
                {
                    FileName = Path.GetFileName(path)
                };

                dialog.ShowDialog(this);
                if (dialog.FileName != "")
                {
                    using (FileStream output = new FileStream(dialog.FileName, FileMode.Create))
                    {
                        Stream downloadStream = Proxy.Download(path);
                        byte[] bt = ReadBytestoStream(downloadStream);
                        output.Write(bt, 0, bt.Length);
                        MessageBox.Show("hi");
                    }

                    Process.Start(dialog.FileName);
                }
                ((IClientChannel)Proxy).Close();

            }
        }

        public static byte[] ReadBytestoStream(Stream input)
        {
            byte[] buffer = new byte[16 * 1024];
            using (MemoryStream ms = new MemoryStream())
            {
                int read;
                while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }
                return ms.ToArray();
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            ////////////////////////////////////
            NetTcpBinding binding = new NetTcpBinding();
            binding.TransferMode = TransferMode.Streamed;
            binding.MaxReceivedMessageSize = 200000000000000;
            binding.SendTimeout = TimeSpan.MaxValue;

            EndpointAddress address = new EndpointAddress("net.tcp://localhost:5000/DataUploader");

            ChannelFactory<IDataUploader> channel =
                new ChannelFactory<IDataUploader>(binding, address);
            IDataUploader Proxy = channel.CreateChannel();
            //////////////////////////////////////////////

            if (FileList.SelectedItems.Count == 0)
            {
                MessageBox.Show("No File is Selected");
            }
            else
            {
                ListViewItem item = FileList.SelectedItems[0];

                string path = item.SubItems[0].Text;

                Proxy.Delete(path);
                RefreshFileList();
            }
        }

        private void frmClient_Load(object sender, EventArgs e)
        {

        }
    }

    } 
        



    